package com.lmabbe.core.api.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lmabbe.common.global.result.R;
import com.lmabbe.core.common.converter.SysResourceConverter;
import com.lmabbe.core.common.dto.SysResourceDTO;
import com.lmabbe.core.common.entitys.SysResource;
import com.lmabbe.core.common.interfaces.SysResourceService;
import com.lmabbe.core.common.query.SysResourceQuery;
import com.lmabbe.core.common.vo.SysResourceTreeVO;
import com.lmabbe.core.common.vo.SysResourceVO;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import lombok.RequiredArgsConstructor;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author lmabbe
 */
@RestController
@RequestMapping("resource")
@Api(tags = "资源控制器")
@RequiredArgsConstructor
public class SysResourceController {

    @DubboReference
    private SysResourceService sysResourceService;
    private final SysResourceConverter sysResourceConverter;

    @GetMapping("tree")
    public R<List<SysResourceTreeVO>> getTree() {
        return R.success(sysResourceService.getTreeList());
    }


    /**
     * 获取所有
     *
     * @return
     */
    @Operation(summary = "获取所有")
    @GetMapping("all")
    public R<List<SysResourceVO>> list() {
        List<SysResource> list = sysResourceService.list();
        return R.success(sysResourceConverter.list2Vos(list));
    }

    /**
     * 分页获取
     *
     * @param query
     * @return
     */
    @Operation(summary = "分页获取")
    @GetMapping("page")
    public R<List<SysResourceVO>> list(SysResourceQuery query) {
        IPage page = sysResourceService.selectPage(query);
        page.setRecords(sysResourceConverter.list2Vos(page.getRecords()));
        return R.page(page);
    }

    /**
     * 获取单个实体类
     *
     * @param id
     * @return
     */
    @Operation(summary = "获取单个实体类")
    @GetMapping("/{id}")
    public R<SysResourceVO> getById(@PathVariable Long id) {
        return R.success(sysResourceConverter.entity2Vo(sysResourceService.getById(id)));
    }

    /**
     * 新增单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增单个实体类")
    @PostMapping()
    public R add(@RequestBody SysResourceDTO dto) {
        this.sysResourceService.save(sysResourceConverter.dto2Entity(dto));
        return R.success();
    }

    /**
     * 修改单个实体类
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改单个实体类")
    @PutMapping("{resourceId}")
    public R update(@RequestBody SysResourceDTO dto, @PathVariable Long resourceId) {
        this.sysResourceService.updateById(sysResourceConverter.dto2Entity(dto, resourceId));
        return R.success();
    }

    /**
     * 删除
     *
     * @param id
     * @return
     */
    @Operation(summary = "删除")
    @DeleteMapping("/{id}")
    public R delete(@PathVariable Long id) {
        this.sysResourceService.removeById(id);
        return R.success();
    }
}
